/**
 * 
 */
package com.jinmei.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jinmei.dao.EmployeeDao;
import com.jinmei.model.Employee;
import com.jinmei.model.ToDoList;

/**
 * @author richard
 *
 */
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
	
	@Override
	public Employee findEmployee(String fNumber) throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Employee.class);
		criteria.add(Restrictions.eq("fNumber", fNumber));
		List<Employee> results = this.getHibernateTemplate().findByCriteria(criteria);
		
		if (results == null || results.size() == 0) {
			return null;
		}
		return results.get(0);
	}

	@Override
	public List<Employee> findAllEmployees() throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Employee.class);
		criteria.addOrder(Order.asc("fNumber"));
//		criteria.add(Restrictions.eq("isDeleted", 0));
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public List<Employee> findEmpolyeeByFNumberAndName(String fNumber,
			String name) throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Employee.class);
		criteria.addOrder(Order.asc("fNumber"));
		if (StringUtils.isEmpty(fNumber) && StringUtils.isEmpty(name)) {
			return this.getHibernateTemplate().findByCriteria(criteria);
		}
		
		if (StringUtils.isEmpty(fNumber)) {
			criteria.add(Restrictions.eq("fName", name));
		} else if (StringUtils.isEmpty(name)) {
			criteria.add(Restrictions.eq("fNumber", fNumber));
		} else {
			criteria.add(Restrictions.eq("fName", name));
			criteria.add(Restrictions.eq("fNumber", fNumber));
		}
		
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public void saveOrUpdateEmployee(Employee employee)
			throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(employee);
	}

	public List<ToDoList> findEmployeeTasksByFid(Integer fId) throws DataAccessException {
		List<ToDoList> tasks = new ArrayList<ToDoList>();
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Employee.class);
		criteria.add(Restrictions.eq("fId", fId));
		List<Employee> employeeList = this.getHibernateTemplate().findByCriteria(criteria);
		if(employeeList != null){
			Employee employee = employeeList.get(0);
			Set<ToDoList> taskSet = employee.getEmployeeTasks();
			Iterator<ToDoList> it = taskSet.iterator();
			while(it.hasNext()){
			   tasks.add(it.next());
			}
		}
		return tasks;
	}

	@Override
	public void deleteEmployee(Employee employee) throws DataAccessException {
		this.getHibernateTemplate().delete(employee);
	}

}
